//
//  LMTabBarController.m
//  LMTabBarControllerKit
//
//  Created by mengmenglu on 6/25/16.
//  Copyright © 2016 Hangzhou TaiXuan Network Technology Co., Ltd. All rights reserved.
//

#import "LMTabBarController.h"
#import "LMTabBar.h"

@interface LMTabBarController ()<LMTabBarDelegate>

@property (nonatomic,strong) NSArray *viewControllerArray;
@property (nonatomic,strong) LMTabBar *customTabBar;

@end

@implementation LMTabBarController

#pragma mark - LifeCycle

- (id)initWithViewControllers:(NSArray *)viewControllers {
    self = [super init];
    if (self) {
        self.viewControllerArray = [viewControllers copy];
        [self setUpView];
    }
    return self;
}


- (void)loadView {
    [super loadView];
    self.tabBar.backgroundColor = [UIColor whiteColor];
    self.tabBar.tintColor = TH27A1E5;
}


- (void)viewDidLoad {
    [super viewDidLoad];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Public Method
- (void)updatetabBarWith:(NSArray *)tabBarItems {
    [self.customTabBar updateTabBarItemsWith:tabBarItems];
}


#pragma mark - Private Method
- (void)setUpView {
    
    if ([self.viewControllers count] >0) {
        return;
    }
    
    NSMutableArray *navigationControllers = [[NSMutableArray alloc] init];
    for (UIViewController *subViewController in self.viewControllerArray) {
        UINavigationController *navCtrl = [self configNavigationControllerWith:subViewController];
        [navigationControllers addObject:navCtrl];
    }
    self.viewControllers = navigationControllers;
    
    [self setUpTabBar];
}


- (void)setUpTabBar {
    [self removeSubView];
    [self.tabBar addSubview:self.customTabBar];
    [self.customTabBar updateTabBarItemsWith:[self tabBarItems]];
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
}


- (void)removeSubView {
    // 清空系统的控件
    if ([self.tabBar.subviews count] >0) {
        for (UIView *subView in self.tabBar.subviews) {
            [subView removeFromSuperview];
        }
    }
}


- (UINavigationController *)configNavigationControllerWith:(UIViewController *)viewController {
    if ([viewController isKindOfClass:[UINavigationController class]]) {
        return (UINavigationController *)viewController;
    }
    
    if ([viewController isKindOfClass:[UIViewController class]]) {
        UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewController];
        return navCtrl;
    }
    
    UIViewController *newViewController = [[UIViewController alloc] init];
    UINavigationController *newNavCtrl = [[UINavigationController alloc] initWithRootViewController:newViewController];
    return newNavCtrl;
}


- (void)removeSubViewControllerWith:(NSArray *)viewControllers {
    for (UIViewController *subViewConrtroller in viewControllers) {
        [subViewConrtroller removeFromParentViewController];
    }
}


#pragma mark - LMTabBarDelegate
- (void)tabBar:(LMTabBar *)tabBar selectedItem:(LMTabBarItem *)selectedItem index:(NSInteger)index {
    self.selectedIndex = index;
}


#pragma mark - UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewControlle {
    return YES;
}


#pragma mark - Getter Method
- (LMTabBar *)customTabBar {
    if (!_customTabBar) {
        _customTabBar = [[LMTabBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, TABBARITEM_HEIGHT)];
        _customTabBar.delegate = self;
    }
    return _customTabBar;
}


- (NSArray *)tabBarItems {
    
    LMTabBarItemModel *tabBarItemModel1 = [[LMTabBarItemModel alloc] initWithTitle:@"title1" normalTextColor:[UIColor blackColor] selectTextColor:TH27A1E5 normalImage:[UIImage imageNamed:@"trHomeDown_normal.png"] selectedImage:[UIImage imageNamed:@"trHomeDown_selected.png"] normalImageSize:CGSizeZero selectedImageSize:CGSizeMake(40, 40) normalImageUrl:nil selectImageUrl:nil];
    LMTabBarItemModel *tabBarItemModel2 = [[LMTabBarItemModel alloc] initWithTitle:@"title2" normalTextColor:[UIColor blackColor]  selectTextColor:TH27A1E5 normalImage:[UIImage imageNamed:@"trList_normal.png"] selectedImage:[UIImage imageNamed:@"trList_selected.png"] normalImageSize:CGSizeZero selectedImageSize:CGSizeMake(40, 40) normalImageUrl:nil selectImageUrl:nil];
    LMTabBarItemModel *tabBarItemModel3 = [[LMTabBarItemModel alloc] initWithTitle:@"title3" normalTextColor:[UIColor blackColor]  selectTextColor:TH27A1E5 normalImage:[UIImage imageNamed:@"trAccount_normal.png"] selectedImage:[UIImage imageNamed:@"trAccount_selected.png"] normalImageSize:CGSizeZero selectedImageSize:CGSizeMake(40, 40) normalImageUrl:nil selectImageUrl:nil];
    LMTabBarItemModel *tabBarItemModel4 = [[LMTabBarItemModel alloc] initWithTitle:@"title4" normalTextColor:[UIColor blackColor]  selectTextColor:TH27A1E5 normalImage:[UIImage imageNamed:@"trMore_normal.png"] selectedImage:[UIImage imageNamed:@"trMore_selected.png"] normalImageSize:CGSizeZero selectedImageSize:CGSizeMake(40, 40) normalImageUrl:nil selectImageUrl:nil];
    
    LMTabBarItem *tabBarItem1 = [[LMTabBarItem alloc] init];
    tabBarItem1.tabBarItemModel = tabBarItemModel1;
    LMTabBarItem *tabBarItem2 = [[LMTabBarItem alloc] init];
    tabBarItem2.tabBarItemModel = tabBarItemModel2;
    LMTabBarItem *tabBarItem3 = [[LMTabBarItem alloc] init];
    tabBarItem3.tabBarItemModel = tabBarItemModel3;
    LMTabBarItem *tabBarItem4 = [[LMTabBarItem alloc] init];
    tabBarItem4.tabBarItemModel = tabBarItemModel4;
    return@[tabBarItem1,tabBarItem2,tabBarItem3,tabBarItem4];
}



@end
